package chapter5_stack_and_linked;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yuh
 * @date 2018/4/12 15:03
 * @description list切分
 */
public class ListSplit {

    /**
     * 集合切分 不足1/3时组装进最后一个分量
     * @param list 原始集合
     * @param count 切分后每个结合集合数量
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> split(List<T> list, int count) {
        LinkedList<List<T>> rtList = new LinkedList<>();
        int originalSize = list.size();
        if (originalSize <= count) {
            rtList.addLast(list);
        } else {
            int part = originalSize / count;
            int last = originalSize % count;
            if (last >= count / 3) {
                part += 1;
            }
            int start = 0;
            for (int i = 1; i <= part; i++) {
                 start = (i - 1) * count;
                 rtList.addLast(list.subList(start,Math.min(start+count,originalSize)));
            }
        }
        return rtList;
    }


    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            list.add(i);
        }
        System.out.println(split(list, 600));
    }

}
